package cn.edu.cug.cs.gtl.gsm.aof.ftp;

import cn.edu.cug.cs.gtl.gsm.aof.IndexFile;
import cn.edu.cug.cs.gtl.gsm.item.IndexMap;
import cn.edu.cug.cs.gtl.gsm.item.MetaItem;
import org.apache.commons.net.ftp.FTPClient;

import java.io.*;

public class FTPIndexFile implements IndexFile {
    private String fileName = "ftp_sm_index";
    private FTPClient ftpClient;
    private String workDirectory;
    private MetaItem metaItem;
    private IndexMap pageMap;

    public FTPIndexFile(FTPClient ftpClient, String workDirectory, MetaItem metaItem, IndexMap pageMap) {
        this.ftpClient = ftpClient;
        this.workDirectory = workDirectory;
        this.metaItem = metaItem;
        this.pageMap = pageMap;
        this.fileName = "ftp_sm_index_" + this.metaItem.getBaseName();
    }

    /**
     * @return
     * @throws IOException
     */
    @Override
    public boolean create() throws IOException {
        //在FTP上的workDirectory+baseName目录下创建文件fileName
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        OutputStream outputStream = ftpClient.storeFileStream(fullPathName);
        outputStream.close();
        ftpClient.completePendingCommand();
        return true;
    }

    @Override
    public boolean drop() throws IOException {
        //在FTP上的workDirectory+baseName目录下删除文件fileName
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        return ftpClient.deleteFile(fullPathName);
    }

    @Override
    public boolean exists() throws IOException {
        //在FTP上的workDirectory+baseName目录下判断文件fileName是否存在
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        return FTPStorageManager.existsFile(ftpClient,fullPathName);
    }

    @Override
    public boolean write(MetaItem metaItem, IndexMap indexMap) throws IOException {
        //在FTP上的workDirectory+baseName目录下打开文件fileName，并写入metaItem和indexMap
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        OutputStream outputStream = ftpClient.storeFileStream(fullPathName);
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        byte [] bs = metaItem.storeToCompressBytes();
        dataOutputStream.writeInt(bs.length);
        dataOutputStream.write(bs);
        bs = pageMap.storeToCompressBytes();
        dataOutputStream.writeInt(bs.length);
        dataOutputStream.write(bs);
        outputStream.close();
        dataOutputStream.close();
        ftpClient.completePendingCommand();
        return true;
    }

    @Override
    public boolean read(MetaItem metaItem, IndexMap indexMap) throws IOException {
        //在FTP上的workDirectory+baseName目录下打开文件fileName，并读出metaItem和indexMap
        String fullPathName = FTPStorageManager.concatenate(this.workDirectory,this.metaItem.getBaseName(),this.fileName);
        ftpClient.setRestartOffset(0);
        InputStream inputStream = ftpClient.retrieveFileStream(fullPathName);
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        int c = dataInputStream.readInt();
        byte []bs = new byte[c];
        dataInputStream.read(bs);
        metaItem.loadFromCompressBytes(bs);
        c = dataInputStream.readInt();
        bs = new byte[c];
        dataInputStream.read(bs);
        indexMap.loadFromCompressBytes(bs);
        inputStream.close();
        dataInputStream.close();
        ftpClient.completePendingCommand();
        return true;
    }
}
